package com.qpcy.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qpcy.dto.TypeDto;
import com.qpcy.entity.Product;
import com.qpcy.entity.Type;
import com.qpcy.mapper.HotProductMapper;
import com.qpcy.mapper.ProductMapper;
import com.qpcy.mapper.TypeMapper;
import com.qpcy.service.TypeService;
import com.qpcy.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import static com.qpcy.entity.table.TypeTableDef.TYPE;
import static com.qpcy.entity.table.ProductTableDef.PRODUCT;
import static com.qpcy.entity.table.HotProductTableDef.HOT_PRODUCT;

/**
 * 分类 服务层实现。
 *
 * @author LENOVO
 * @since 2024-10-19
 */
@Service
@Transactional
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private HotProductMapper hotProductMapper;


    @Override
    public ResponseResult deleteById(int typeId) {
        System.out.println("typeId = " + typeId);
        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(TYPE.PARENT_ID.eq(typeId));
        List<Type> types = typeMapper.selectListByQuery(queryWrapper);
        List<Integer> collect = types.stream().map(Type::getTypeId).collect(Collectors.toList());
        collect.add(typeId);
        System.out.println("collect = " + collect);
        typeMapper.deleteBatchByIds(collect);

        QueryWrapper queryWrapper1 = QueryWrapper.create()
                .where(PRODUCT.TYPE_ID.eq(typeId));
        List<Product> products = productMapper.selectListByQuery(queryWrapper1);
        System.out.println("products = " + products);
        List<Integer> productIds = products.stream().map(Product::getProductId).collect(Collectors.toList());
        System.out.println("productIds = " + productIds);
        productMapper.deleteByQuery(queryWrapper1);
        if(!productIds.isEmpty()) {
            QueryWrapper queryWrapper2 = QueryWrapper.create()
                    .where(HOT_PRODUCT.PRODUCT_ID.in(productIds));
            hotProductMapper.deleteByQuery(queryWrapper2);
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult getAll() {
        List<Type> types = typeMapper.selectAll();

        List<TypeDto> typeDtoList = types.stream()
                .map(item -> {
                    TypeDto typeDto = new TypeDto();
                    BeanUtils.copyProperties(item, typeDto);
                    return typeDto;
                }).collect(Collectors.toList());

        List<TypeDto> resultList = typeDtoList.stream()
                .filter(item -> item.getParentId() == 0)
                .sorted(Comparator.comparing(TypeDto::getSort,
                        Comparator.nullsLast(Comparator.naturalOrder())))
                .collect(Collectors.toList());

        for (TypeDto typeDto : resultList) {
            List<TypeDto> list = new ArrayList<>();
            for (TypeDto dto : typeDtoList) {
                if (Objects.equals(dto.getParentId(), typeDto.getTypeId())) {
                    list.add(dto);
                }
            }
            list.sort(Comparator.comparing(TypeDto::getSort,
                    Comparator.nullsLast(Comparator.naturalOrder())));
            typeDto.setChildren(list);
        }

        return ResponseResult.success(resultList);
    }

}
